<!--

    Copyright © 2016-2023 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<fieldset [formGroup]="entityTypesVersionCreateFormGroup" class="fields-group" fxLayout="column">
  <legend class="group-title" translate>version-control.entities-to-export</legend>
  <div fxLayout="column" style="flex: 1; overflow: auto;">
    <div *ngFor="let entityTypeFormGroup of entityTypesFormGroupArray(); trackBy: trackByEntityType;
             let $index = index; last as isLast;"
         fxLayout="row" fxLayoutAlign="start center" [ngStyle]="!isLast ? {paddingBottom: '8px'} : {}">
      <mat-expansion-panel class="entity-type-config" [ngClass]="{'load': loading}" fxFlex [formGroup]="entityTypeFormGroup"
                           [expanded]="entityTypesFormGroupExpanded(entityTypeFormGroup)" (opened)="loading = false">
        <mat-expansion-panel-header>
          <div fxFlex fxLayout="row" fxLayoutAlign="start center">
            <mat-panel-title>
              <div fxLayout="row" fxFlex fxLayoutAlign="start center">
                <div>{{ entityTypeText(entityTypeFormGroup) }}</div>
              </div>
            </mat-panel-title>
            <span fxFlex></span>
            <button *ngIf="!disabled" mat-icon-button style="min-width: 40px;"
                    type="button"
                    (click)="removeEntityType($index)"
                    matTooltip="{{ 'action.remove' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>delete</mat-icon>
            </button>
          </div>
        </mat-expansion-panel-header>
        <div class="entity-type-config-content" fxLayout="column" fxLayoutGap="0.5em">
          <mat-divider></mat-divider>
          <div fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="16px">
            <tb-entity-type-select
              showLabel
              formControlName="entityType"
              required
              [filterAllowedEntityTypes]="false"
              [allowedEntityTypes]="allowedEntityTypes(entityTypeFormGroup)">
            </tb-entity-type-select>
            <div fxFlex fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="16px" formGroupName="config">
              <mat-form-field fxFlex class="mat-block">
                <mat-label translate>version-control.sync-strategy</mat-label>
                <mat-select formControlName="syncStrategy">
                  <mat-option [value]="'default'">
                    {{ 'version-control.default' | translate }}
                  </mat-option>
                  <mat-option *ngFor="let strategy of syncStrategies" [value]="strategy">
                    {{syncStrategyTranslations.get(strategy) | translate}}
                  </mat-option>
                </mat-select>
              </mat-form-field>
              <div fxFlex fxLayout="column">
                <mat-checkbox *ngIf="entityTypeFormGroup.get('entityType').value === entityTypes.DEVICE" formControlName="saveCredentials">
                  {{ 'version-control.export-credentials' | translate }}
                </mat-checkbox>
                <mat-checkbox formControlName="saveAttributes">
                  {{ 'version-control.export-attributes' | translate }}
                </mat-checkbox>
                <mat-checkbox formControlName="saveRelations">
                  {{ 'version-control.export-relations' | translate }}
                </mat-checkbox>
              </div>
            </div>
          </div>
          <div fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="16px" fxLayoutAlign.gt-xs="start center"
               formGroupName="config" ngStyle.gt-xs="min-height: 76px;">
            <mat-slide-toggle formControlName="allEntities">
              {{ 'version-control.all-entities' | translate }}
            </mat-slide-toggle>
            <tb-entity-list
              fxFlex
              [fxShow]="!entityTypeFormGroup.get('config').get('allEntities').value"
              [entityType]="entityTypeFormGroup.get('entityType').value"
              required
              formControlName="entityIds">
            </tb-entity-list>
          </div>
        </div>
      </mat-expansion-panel>
    </div>
    <div *ngIf="!entityTypesFormGroupArray().length">
          <span translate fxLayoutAlign="center center"
                class="tb-prompt">version-control.no-entities-to-export-prompt</span>
    </div>
  </div>
  <div style="padding-top: 16px;" fxLayout="row">
    <button mat-raised-button color="primary"
            type="button"
            [disabled]="!addEnabled()"
            (click)="addEntityType()">
      <span translate>version-control.add-entity-type</span>
    </button>
    <span fxFlex></span>
    <button mat-raised-button color="primary"
            type="button"
            [disabled]="!entityTypesFormGroupArray().length"
            (click)="removeAll()">
      <span translate>version-control.remove-all</span>
    </button>
  </div>
</fieldset>
